#include "GModel.h"
#include "GShader.h"
#include "OBJ_Loader.h"

GModel::GModel()
{
	m_pMaterial = NULL;
}

int GModel::nFaces()
{
	return m_lstFaces.size();
}

void GModel::LoadObj(std::string szPath)
{
	objl::Loader Loader;
	Loader.LoadFile(szPath);
	for (auto mesh : Loader.LoadedMeshes)
	{
		for (int i = 0; i < mesh.Vertices.size(); i += 3)
		{
			Vector3f vec3VertexPos;
			Vector3f vec3Normal;
			Vector2f vec2UV;
			std::vector<int> face;
			for (int j = 0; j < 3; ++j)
			{
				vec3VertexPos[0] = mesh.Vertices[i + j].Position.X;
				vec3VertexPos[1] = mesh.Vertices[i + j].Position.Y;
				vec3VertexPos[2] = mesh.Vertices[i + j].Position.Z;

				vec3Normal[0] = mesh.Vertices[i + j].Normal.X;
				vec3Normal[1] = mesh.Vertices[i + j].Normal.Y;
				vec3Normal[2] = mesh.Vertices[i + j].Normal.Z;

				vec2UV[0] = mesh.Vertices[i + j].TextureCoordinate.X;
				vec2UV[1] = mesh.Vertices[i + j].TextureCoordinate.Y;

				m_lstVerts.push_back(vec3VertexPos);
				m_lstNormals.push_back(vec3Normal);
				m_lstUVs.push_back(vec2UV);
				face.push_back(m_lstVerts.size() - 1);
			}
			m_lstFaces.push_back(face);
		}
	}
}

void GModel::LoadFace(Payload& payload, int nFaceIndex)
{
	std::vector<int> face_vert_index = m_lstFaces[nFaceIndex];
	for (int i = 0; i < face_vert_index.size(); ++i)
	{
		int nVertexIndex = face_vert_index[i];
		Vector3f vec3Pos = m_lstVerts[nVertexIndex];
		VertexProp* pVertexProp = &payload.in_vertexProp[i];
		pVertexProp->vertex_pos = Vector4f(vec3Pos.x(), vec3Pos.y(), vec3Pos.z(), 1.0f);
		pVertexProp->uv = m_lstUVs[nVertexIndex];
		pVertexProp->normal = m_lstNormals[nVertexIndex];
	}
}

Matrix4f GModel::GetModelMatrix()
{
	return Matrix4f::Identity();
}